export default {
    name:"MySelectTreeDlg",
    template: `<my-dialog :title="title" v-model="dlgVisible">
        <el-tree ref="tree" highlight-current
            check-on-click-node
            style="width: 100%; min-height:400px;"
            :props="props" :load="loadNode" :lazy="lazy" />
        <template #footer>
            <el-button @click="dlgVisible = false">取消</el-button>
            <el-button v-if="clearable" type="success" @click="onClear">清除</el-button>
            <el-button type="primary" @click="onOk">确定</el-button>
        </template>
    </my-dialog>`,
    props: {
        clearable: {
            type:Boolean,
            default:true,
        },
        title: {
            type: String,
            default:"选择节点",
        },
        action: {
            type: String,
            required: true,
        },
        valueField: {
            type: String,
            default:"value",
        },
        labelField: {
            type: String,
            default:"label",
        },
        childrenField: {
            type: String,
            default: "children",
        },
        cached: Boolean,
    },
    emits: ["selected", "clear"],
    data() {
        return {
            lazy:true,
            dlgVisible: false,
            props: {
                label: this.labelField,
                children: this.childrenField,
                isLeaf: 'leaf',
            }
        }
    },
    methods: {
        open() {
            this.dlgVisible = true;
        },
        close() {
            this.dlgVisible = false;
        },
        reduceTree(nodes){
            nodes.forEach(x=>{
                if(x.children && x.children.length>0){
                    this.reduceTree(x.children)
                }
                if(x.children==null){
                    x.isLeaf = true;
                }
            })
        },
        loadNode(node, resolve, reject) {
            if(!this.lazy){
                resolve( node?.data?.children || []);
                return;
            }
            let pm = { pid: node?.data[this.valueField], pageIndex:1, pageSize:10000 };
            $server.call(this.action, pm).then(res => {
                if(res.records && res.records.length>0){
                    res = res.records;
                }
                //如果节点返回数据中存在children或isLeaf字段，则认为不是懒加载
                if(res.length>0){
                    this.lazy =  !(res[0]["children"] || res[0]["isLeaf"]);
                }
                this.reduceTree(res);
                resolve(res);
            }).catch(e => {
                reject(e);
            });
        },
        onOk() {
            var node = this.$refs.tree.getCurrentNode();
            if (!node) return;
            var path = [];
            var tnode = this.$refs.tree.getNode(node.$treeNodeId);
            while (tnode != null) {
                let lb = tnode.data[this.labelField];
                if (!lb) break;
                path.push(lb);
                tnode = tnode.parent;
            }
            node = {
                ...node,
                fullPath: path.reverse().join("/")
            };
            if (node != null) {
                this.$emit("selected", node);
                this.close();
            }
        },
        onClear() {
            this.$emit("clear");
            this.close();
        }
    }
}